import requests
import json

from django.conf import settings


# from .exception import ValidationError


class MyRequests:
    def __init__(self, client_id):
        self.userName = 'admin'
        self.password = 'public'
        self.headers = {
            'content-type': 'application/json',
            'User-Agent': '0.0.1',
            "application/json;charset": "utf-8",
        }

    def is_file_exit(self, file_path):
        url_front = settings.IS_FILE.format("".join(["file_path", "=", file_path]))
        r = requests.get(url_front, headers=self.headers)
        status_code = r.status_code
        if status_code == 200:
            content = r.content.decode("utf-8")
            content = json.loads(content)
            is_file = content.get("is_file")
            if is_file:
                return True
            else:
                return False
        return False

    def get_md5(self, file_path, is_new):
        url_front = settings.GET_MD5.format("".join(["file_path", "=", file_path]), "".join(["is_new", "=", is_new]))
        r = requests.get(url_front, headers=self.headers)
        status_code = r.status_code
        if status_code == 200:
            content = r.content.decode("utf-8")
            content = json.loads(content)
            return content
        return {}

    def remove_file(self, file_path):
        url_front = settings.REMOVEFILE.format("".join(["file_path", "=", file_path]))
        r = requests.get(url_front, headers=self.headers)

    def ana_file(self, file_path):
        url_front = settings.ANA_FILE.format("".join(["file_path", "=", file_path]))
        r = requests.get(url_front, headers=self.headers)
        status_code = r.status_code
        if status_code == 200:
            content = r.content.decode("utf-8")
            content = json.loads(content)
            return content
        return {}

    def fresh_history(self, data):
        gateway_id = data.get("gateway_id")
        is_connected = data.get("is_connected")
        url_front = settings.HISTORY_FRESH.format("gateway_id={}&is_connected={}".format(gateway_id, is_connected))
        r = requests.get(url_front, headers=self.headers)
        status_code = r.status_code
        return status_code

    def awake_gateway(self, data):
        gateway_id = data.get("gateway_id")
        url_front = settings.AWAKE_GATEWAY.format("gateway_id={}".format(gateway_id))
        r = requests.get(url_front, headers=self.headers)
        status_code = r.status_code
        return status_code


my_request = MyRequests("yvan_test")
